<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\node\model;

class RoleList extends \app\BaseModel
{
    protected $name = "massage_role_list";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        if(isset($data["node"])) {
            $node = $data["node"];
            unset($data["node"]);
        }
        $res = $this->insert($data);
        $id = $this->getLastInsID();
        if(isset($node)) {
            $this->updateSome($node, $id, $data["uniacid"]);
        }
        return $id;
    }
    public function updateSome($data, $id, $uniacid)
    {
        $node_model = new RoleNode();
        $node_model->where(["role_id" => $id])->delete();
        if(!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]["uniacid"] = $uniacid;
                $data[$k]["role_id"] = $id;
                $data[$k]["auth"] = !empty($v["auth"]) ? implode(",", $v["auth"]) : "";
            }
            $node_model->saveAll($data);
        }
        return true;
    }
    public function dataUpdate($dis, $data)
    {
        if(isset($data["node"])) {
            $node = $data["node"];
            unset($data["node"]);
        }
        $res = $this->where($dis)->update($data);
        if(isset($node)) {
            $this->updateSome($node, $dis["id"], $data["uniacid"]);
        }
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateOne($id)
    {
        $user_id = $this->where(["id" => $id])->value("user_id");
        $res = $this->where(["user_id" => $user_id])->where("id", "<>", $id)->update(["status" => 0]);
        return $res;
    }
    public function checkDatas($uniacid)
    {
        $p = new \app\node\info\PermissionNode($uniacid);
        $auth_num = $p->getAuthNumber();
        $num = $this->where(["uniacid" => $uniacid])->where("status", ">", -1)->count();
        if($auth_num <= $num) {
            return ["code" => 500, "msg" => "授权数量不足" . $auth_num . "-" . $num];
        }
        return true;
    }
    public function getShopOrderRole($uniacid)
    {
        $dis = ["a.uniacid" => $uniacid, "a.status" => 1, "b.node" => "ShopOrder"];
        $list = $this->alias("a")->join("massage_role_node b", "a.id = b.role_id")->where($dis)->field("b.*")->group("b.id")->select()->toArray();
        $arr = [];
        if(!empty($list)) {
            foreach ($list as $value) {
                $auth = !empty($value["auth"]) ? explode(",", $value["auth"]) : [];
                if(in_array("operAbnormal", $auth)) {
                    $arr[] = $value["role_id"];
                }
            }
        }
        $role_admin_mdoel = new RoleAdmin();
        $admin_id = $role_admin_mdoel->where(["uniacid" => $uniacid])->where("role_id", "in", $arr)->column("admin_id");
        return $admin_id;
    }
    public function getUserRole($user_id)
    {
        $admin_model = new \app\massage\model\Admin();
        $admin = $admin_model->dataInfo(["id" => $user_id]);
        if(!empty($admin)) {
            if($admin["is_admin"] == 0) {
                return "代理商";
            }
            if($admin["is_admin"] == 0) {
                return "平台";
            }
        }
        $dis = ["b.admin_id" => $user_id, "a.status" => 1];
        $data = $this->alias("a")->join("massage_role_admin b", "a.id = b.role_id")->where($dis)->group("a.id")->column("a.title");
        return !empty($data) ? implode(",", $data) : "";
    }
}

?>